package com.zzyl.nursing.controller;

import com.zzyl.common.constant.CacheConstants;
import com.zzyl.nursing.service.IDeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/device/debug")
@Slf4j
public class DeviceDebugController {
    
    @Autowired
    private IDeviceService deviceService;
    
    @GetMapping("/syncProduct")
    public String syncProduct() {
        log.info("🔄 手动调用同步产品列表接口");
        try {
            deviceService.syncProductList();
            return "同步完成，请查看控制台日志";
        } catch (Exception e) {
            log.error("同步失败", e);
            return "同步失败: " + e.getMessage();
        }
    }
    
    @GetMapping("/checkRedis")
    public String checkRedis() {
        log.info("🔍 检查Redis数据");
        try {
            String data = redisTemplate.opsForValue().get(CacheConstants.IOT_ALL_PRODUCT_LIST);
            if (data != null && !data.isEmpty()) {
                log.info("✅ Redis中有数据，长度: {}", data.length());
                return "Redis中有数据，长度: " + data.length();
            } else {
                log.info("❌ Redis中无数据");
                return "Redis中无数据";
            }
        } catch (Exception e) {
            log.error("检查Redis失败", e);
            return "检查失败: " + e.getMessage();
        }
    }
    
    @Autowired
    private StringRedisTemplate redisTemplate;
}